package com.education.business.service.education;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.business.mapper.education.SubjectInfoMapper;
import com.education.business.service.BaseService;
import com.education.common.model.PageInfo;
import com.education.model.dto.AdminUserSession;
import com.education.model.dto.SubjectInfoDto;
import com.education.model.entity.SubjectInfo;
import com.education.model.entity.SystemAdmin;
import com.education.model.entity.SystemRole;
import com.education.model.request.PageParam;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zengjintao
 * @version 1.0
 * @create_at 2020/11/19 13:53
 */
@Service
public class SubjectInfoService extends BaseService<SubjectInfoMapper, SubjectInfo> {

    public PageInfo<SubjectInfoDto> selectPageList(PageParam pageParam, SubjectInfo subjectInfo) {
        Page<SubjectInfoDto> page = new Page<>(pageParam.getPageNumber(), pageParam.getPageSize());
        // 获取当前登陆人ID
        Integer adminUserId = getAdminUserId();

        // 获取管理员用户会话
        AdminUserSession adminUserSession = getAdminUserSession();

        SystemAdmin systemAdmin = adminUserSession.getSystemAdmin();
        // 获取当前管理员用户的角色列表
        List<SystemRole> roleList = adminUserSession.getRoleList();

        if(systemAdmin != null && (systemAdmin.getSuperFlag() == 1)){
            adminUserId = null;
        }
//        else if(roleList.stream().anyMatch(role -> role.getId().equals(4))){
//            adminUserId = null;
//        }
        return selectPage(baseMapper.selectPageList(page, subjectInfo,adminUserId));
    }

    public void addOrUpadte(SubjectInfo subjectInfo) {
        subjectInfo.setCreateId(getAdminUserId()); // 创建人
        super.saveOrUpdate(subjectInfo);
    }
}
